package com.shujia

import com.shujia.Demo11CaseClass.CaseStu

object Demo20Match {
  def main(args: Array[String]): Unit = {
    // 1、匹配基本数据类型
    val i: Int = 2

    val returnV: Int = i match {
      case 0 =>
        println(i)
        0 * i
      case 1 =>
        println(i)
        1 * i
      case 2 =>
        println(i)
        2 * i
      case _ => // 相当于Java中的default
        println(i)
        -1
    }

    println(returnV)

    // 2、匹配字符串
    val str: String = "oneffff"
    val r: Int = str match {
      case "one" =>
        1
      case "two" =>
        2
      case "three" =>
        3
      case _ =>
        -1
    }

    println(r)

    // 3、匹配枚举
    val friday: Week.Value = Week.Friday
    friday match {
      case Week.Friday =>
        println("可以愉快度过周末了")
      case Week.Monday =>
        println("可以愉快地摸鱼了")
      case _ =>
        println("其他打工的日子")
    }

    // 4、匹配类型
    val any: Any = 1
    any match {
      case i: Int =>
        println(s"any变量是Int类型，值为$i")
      case s: String =>
        println(s"any变量是String类型，值为$s")
      case _ =>
        println("其他类型")

    }

    val list: List[(String, (String, (Int, (String, String))))] = List[(String, (String, (Int, (String, String))))](
      ("001", ("zs", (20, ("男", "文科一班"))))
      , ("002", ("ls", (21, ("女", "文科二班"))))
      , ("003", ("ww", (20, ("男", "文科三班"))))
    )
    // 提取层层嵌套的数据 最终转换成字符串进行输出
    list.map(t2 => {
        val id: String = t2._1
        val name: String = t2._2._1
        val age: Int = t2._2._2._1
        val gender: String = t2._2._2._2._1
        val clazz: String = t2._2._2._2._2
        s"$id,$name,$age,$gender,$clazz"
      })
      .foreach(println)

    // 借助模式匹配 简化提取嵌套数据的过程
    list.map {
      case (id: String, (name: String, (age: Int, (gender: String, clazz: String)))) =>
        s"$id,$name,$age,$gender,$clazz"
    }.foreach(println)

    // 5、匹配样例类对象
    val zs: CaseStu = CaseStu("001", "zs", 20, "男", "一班")

    zs match {
      case CaseStu("001", "zs", 20, "男", "一班") =>
        println("这是zs")
      case CaseStu("001", "zs", 20, "女", "一班") =>
        println("这是女的zs")
      case CaseStu("002", "ls", 20, "男", "一班") =>
        println("这是ls")
      case _ =>
        println("不知道是谁")
    }

  }


}

// 定义枚举
object Week extends Enumeration {
  val Monday: Week.Value = Value("Mon.")
  val TuesDay: Week.Value = Value("Tue.")
  val Wednesday: Week.Value = Value("Wed.")
  val Thursday: Week.Value = Value("Thur.")
  val Friday: Week.Value = Value("Fri.")
  val Saturday: Week.Value = Value("Sat.")
  val Sunday: Week.Value = Value("Sun.")
}

